home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_se / pretty.e < prev    next >
Text File  |  2000-03-25  |  6KB  |  204 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class PRETTY
  17.    --
  18.    -- The `pretty' command.
  19.    --
  20.  
  21. inherit COMMAND_FLAGS;
  22.  
  23. creation make
  24.  
  25. feature {NONE}
  26.  
  27.    command_name: STRING is "pretty";
  28.  
  29.    make is
  30.       do
  31.          small_eiffel.set_pretty_flag;
  32.          if argument_count < 1 then
  33.             system_tools.bad_use_exit(command_name);
  34.          else
  35.             automat;
  36.          end;
  37.       end;
  38.  
  39. feature {NONE}
  40.  
  41.    state: INTEGER;
  42.  
  43.    style: STRING;
  44.  
  45.    class_names: ARRAY[STRING] is
  46.       once
  47.          !!Result.make(1,10);
  48.          Result.clear;
  49.       end;
  50.  
  51.    automat is
  52.       local
  53.          arg: INTEGER;
  54.          a: STRING;
  55.          -- state 0  : nothing done.
  56.          -- state 1  : end.
  57.          -- state 2  : error.
  58.       do
  59.          from
  60.             arg := 1;
  61.          until
  62.             arg > argument_count or else state > 0
  63.          loop
  64.             a := argument(arg);
  65.             if a.item(1) /= '-' then
  66.                class_names.add_last(a);
  67.             elseif is_flag_no_style_warning(a) then
  68.             elseif is_flag_no_warning(a) then
  69.             elseif is_flag_version(a) then
  70.             elseif ("-default").is_equal(a) then
  71.                if style /= Void then
  72.                   error_style(a);
  73.                else
  74.                   fmt.set_default;
  75.                   style := a;
  76.                end;
  77.             elseif ("-zen").is_equal(a) then
  78.                if style /= Void then
  79.                   error_style(a);
  80.                else
  81.                   fmt.set_zen;
  82.                   style := a;
  83.                end;
  84.             elseif ("-end").is_equal(a) then
  85.                if style /= Void then
  86.                   error_style(a);
  87.                else
  88.                   fmt.set_end;
  89.                   style := a;
  90.                end;
  91.             elseif ("-parano").is_equal(a) then
  92.                if style /= Void then
  93.                   error_style(a);
  94.                else
  95.                   fmt.set_parano;
  96.                   style := a;
  97.                end;
  98.             else
  99.                echo.w_put_string(fz_08);
  100.                echo.w_put_string(a);
  101.                echo.w_put_character('%N');
  102.                state := 2;
  103.             end;
  104.             arg := arg + 1;
  105.          end;
  106.          if nb_errors > 0 then
  107.             eh.append("No pretty printing done.");
  108.             eh.print_as_error;
  109.          else
  110.             if class_names.is_empty then
  111.                eh.append("No Class to Pretty Print.");
  112.                eh.print_as_error;
  113.             else
  114.                pretty_print;
  115.             end;
  116.          end;
  117.       end;
  118.  
  119.    error_style(style2: STRING) is
  120.       do
  121.          state := 2;
  122.          eh.append("pretty: format style is already set to ");
  123.          eh.append(style);
  124.          eh.append(". Bad flag ");
  125.          eh.append(style2);
  126.          eh.append(fz_dot);
  127.          eh.print_as_error;
  128.       end;
  129.  
  130.    pretty_print is
  131.       local
  132.          i: INTEGER;
  133.       do
  134.          from
  135.             i := class_names.lower;
  136.          until
  137.             i > class_names.upper
  138.          loop
  139.             pretty_for(class_names.item(i));
  140.             i := i + 1;
  141.          end;
  142.       end;
  143.  
  144.    pretty_for(name: STRING) is
  145.       require
  146.          name /= Void;
  147.       local
  148.          root_class: STRING;
  149.          e_class: BASE_CLASS;
  150.       do
  151.          run_control.compute_root_class(name);
  152.          root_class := run_control.root_class;
  153.          e_class := small_eiffel.load_class(root_class);
  154.          if e_class = Void then
  155.             eh.append("No pretty printing done for %"");
  156.             eh.append(name);
  157.             fatal_error("%".");
  158.          else
  159.             path.copy(e_class.path);
  160.             backup.copy(path);
  161.             backup.remove_suffix(eiffel_suffix);
  162.             backup.append(backup_suffix);
  163.             if file_exists(backup) then
  164.                eh.append("Old backup file %"");
  165.                eh.append(backup);
  166.                fatal_error("%" already exists.");
  167.             end;
  168.             rename_file(path,backup);
  169.             if not file_exists(backup) then
  170.                eh.append("Cannot rename %"");
  171.                eh.append(path);
  172.                fatal_error("%".");
  173.             end;
  174.             echo.sfw_connect(new_file,path);
  175.             fmt.connect_to(new_file);
  176.             e_class.pretty_print;
  177.             new_file.disconnect;
  178.             if not small_eiffel.re_load_class(e_class) then
  179.                eh.append("Error during `pretty' printing.%N%
  180.                          %Cannot parse output of pretty.%N%
  181.                          %Backup %"");
  182.                eh.append(backup);
  183.                fatal_error("%" not removed.");
  184.             end;
  185.          end;
  186.       end;
  187.  
  188.    path: STRING is
  189.       once
  190.          !!Result.make(256);
  191.       end;
  192.  
  193.    backup: STRING is
  194.       once
  195.          !!Result.make(256);
  196.       end;
  197.  
  198.    new_file: STD_FILE_WRITE is
  199.       once
  200.          !!Result.make;
  201.       end;
  202.  
  203. end -- PRETTY
  204.